package com.ruoyi.web.controller.epidemic;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.epidemic.domain.DeptStaticData;
import com.ruoyi.epidemic.domain.EpidemicControlDept;
import com.ruoyi.epidemic.service.IEpidemicControlDeptService;
import com.ruoyi.epidemic.service.IYqInfoCollectService;
import com.ruoyi.epidemic.service.RequiredWxOauth2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * leader视图
 *
 * @author JINXX
 *
 */
@Controller
@RequestMapping("/wxcp/yqLeaderView")
public class YqLeaderViewController {
	private String prefix = "epidemic/collect";
	@Autowired
	private IYqInfoCollectService yqInfoCollectService;
    protected Logger logger = LoggerFactory.getLogger(YqLeaderViewController.class);

    @Autowired
    private IEpidemicControlDeptService epidemicControlDeptService;
	private static final SimpleDateFormat FORMAT_DATE = new SimpleDateFormat("yyyyMMdd");
	//代表内部人员所在的一级部门,根部门节点为1L
    private static final Long rootDeptId=1L;
	//代表委外人员所在的一级部门,根部门节点为7L
	private static final Long EXTERNAL_ROOT_DEPTID=7L;

	@RequiredWxOauth2
	@GetMapping("leaderview")
	public String leaderview(Model model) {
		//默认首次进入为N，内部人员
		Date today= DateUtils.getNowDate();
		SimpleDateFormat FORMAT_DATE1 = new SimpleDateFormat("yyyy-MM-dd");
		String belongDate=FORMAT_DATE1.format(today);
		model.addAttribute("belongDate", belongDate);
        List<EpidemicControlDept> list=epidemicControlDeptService.selectDeptByparentId(rootDeptId, "N");
        model.addAttribute("list",list);
		List<EpidemicControlDept> listForExternal=epidemicControlDeptService.selectDeptByparentIdForExternal(EXTERNAL_ROOT_DEPTID, "Y");
		model.addAttribute("listForExternal",listForExternal);
		return prefix + "/leaderView";
	}

	/**
	 *  统计视图
	 * @param date
	 * @param deptId
	 * @param flag 内外部员工标识
	 * @return
	 */
	@PostMapping("list")
	@ResponseBody
	public Map<String,Integer> list(String date,String deptId, String flag) {
		String belongDate=date.replaceAll("-","");
		Map<String,Integer> submitcnt=new HashMap<String,Integer>();
		//当前日期, 提交总数
		if(deptId!=null&&!deptId.equals("")){
		    logger.info("deptId:"+deptId);
		    //内部员工
		    if ("-100".equals(deptId)) {
				submitcnt = yqInfoCollectService.getLeaderview(belongDate,Long.valueOf(0L), flag);
			}else if ("-200".equals(deptId)) {
				submitcnt = yqInfoCollectService.getLeaderviewNew(belongDate,Long.valueOf(7L), flag);
			}else {
		    	submitcnt = yqInfoCollectService.getLeaderviewNew(belongDate,Long.valueOf(deptId), flag);
			}
		}
		return submitcnt;
	}

	/**
	 *  统计视图:部门列表详情
	 * @param date
	 * @param deptId
	 * @param flag 内外部员工标识
	 * @return
	 */
	@PostMapping("deptDetailList")
	@ResponseBody
	public Map<String,Object> deptDetailList(String date,String deptId, String flag) {
		String belongDate=date.replaceAll("-","");
		Map<String, Object> result = new HashMap<>();
		//子数据列表
		List<EpidemicControlDept> list = new ArrayList<>();
		//当前日期, 提交总数
		if(deptId!=null&&!deptId.equals("")){
			logger.info("deptId:"+deptId);
			//内部员工
			if ("-100".equals(deptId)) {
				list=epidemicControlDeptService.selectDeptByparentId(rootDeptId, flag);
			}else if ("-200".equals(deptId)) {
				list=epidemicControlDeptService.selectDeptByparentIdForExternal(EXTERNAL_ROOT_DEPTID, flag);
			}else {
				list=epidemicControlDeptService.selectDeptByparentIdForExternal(Long.valueOf(deptId), flag);
			}
			List<DeptStaticData> listData = new ArrayList<>();
			if (CollUtil.isNotEmpty(list)) {
				listData = yqInfoCollectService.getLeaderviewListByDeptIds(belongDate, flag, list);
			}
			result.put("list", listData);
		}
		return result;
	}

	/**
	 * 导出数据类型  1:未填报(内部) 2:总提交人(内部) 3: 总提交人(外部)
	 * @param request
	 * @param response
	 */
	@GetMapping("export")
	public void exportExcel(HttpServletRequest request, HttpServletResponse response){
		String belongDate = request.getParameter("date").replaceAll("-","");
		String deptId = request.getParameter("deptId");
		String dataType = request.getParameter("dataType");
		yqInfoCollectService.downExcelFile(Convert.toLong(deptId), dataType, belongDate, response);
	}
}
